package com.boot2.core.web;

import com.boot2.core.utils.HttpsClient;
import com.boot2.core.utils.ServletUtil;
import lombok.extern.apachecommons.CommonsLog;
import org.apache.commons.lang3.ArrayUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author zhangweilin
 * @Description: xxxxx
 * @date 2020/5/30
 */
@Component
@CommonsLog
public class RestartedSuccessHandler_bak {

    @Autowired
    Environment environment;

    /**
     * 外网ip
     */
    private String extranetIP;

    /**
     * 内网ip
     */
    private String intranetIP;

    /**
     * profiles
     */
    private String[] activeProfiles;

    /**
     * 应用名
     */
    private String appName;

    private static final String reg = "<div class=\"well\"><p>您现在的 IP.*</p></div>";

    @Async
    @Test
    public void hand() {

        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("User-Agent", "Mozilla/5.0(Macintosh;IntelMacOSX10_7_0)AppleWebKit/535.11(KHTML,likeGecko)Chrome/17.0.963.56Safari/535.11");
        String content = HttpsClient.getWithHeader("https://www.ip.cn/", headerMap, Collections.emptyMap());
        Pattern pattern = Pattern.compile(reg);
//        Matcher matcher = pattern.matcher("mmmmmmmmm<title>您的IP地址是：120.204.161.103</title>ppppppppppp");
        Matcher matcher = pattern.matcher(content);
        if (matcher.find()) {
            extranetIP = matcher.group(0);
        }

        intranetIP = ServletUtil.getIpAddr();
        activeProfiles = environment.getActiveProfiles();
        appName = environment.getProperty("spring.application.name");

        StringBuffer sb = new StringBuffer();
        sb.append("恭喜，只是告诉你，重启成功，不用紧张不用紧张");
        sb.append("<br/><b><font size=\"20\" color=\"green\">首先，恭喜，这不是异常信息，而是告诉你，系统重启成功了，不用紧张不用紧张</font><b>");
        sb.append("<br/>应用名称:【").append(appName).append("】");
        sb.append("<br/>环境:【").append(ArrayUtils.toString(activeProfiles)).append("】");
        sb.append("<br/>外网IP:【").append(extranetIP).append("】");
        sb.append("<br/>内网IP:【").append(intranetIP).append("】");
        sb.append("<br/>当前时间:").append(new Date().toLocaleString());

        log.error(sb);

    }

}

